使用消息队列实现进程间的通信(代码实例)

您所在的位置:网站首页 java编程实例 进程和父进程终止 使用消息队列实现进程间的通信(代码实例)

使用消息队列实现进程间的通信(代码实例)

2024-06-16 17:53| 来源: 网络整理| 查看: 265

分别编写代表两个人的程序,他们之间用消息队列进行通信,注意箭头是双向的!注意: 1. Jack与Rose必须可以同时收与发 2. Jack发送了“quit”给对方,自己与Rose都要退出 3. Rose发送了”quit”给对方,,自己与Jack都要退出 信息 信息 Jack Rose 什么是消息队列?

**消息队列是一种进程间通信的方式,是IPC对象的一种,使用前需要申请对应的资源(key值与ID号)。 消息队列使用特定的函数:msgsnd(),msgrcv() 来收发数据,可以读取特定的数据。 消息队列数据有以下特点: 1.消息队列的数据都包括"标识+正文"。 2.往消息队列中写入数据时,必须提供"标识+正文" 3.从消息队列中读取数据时,必须提供想读取的标识 数据: struct data { long mtype; //消息的标识,必须是long类型 char mtext[1]; //消息的正文,可以是任意类型,且大小自定 }; 消息队列有以下接口: //1.根据key值申请消息队列ID号 int msgget(key_t key, int msgflg); //2.向消息队列发送数据 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); //3. 从消息队列中读取数据 ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); //4.设置或获取消息队列的相关属性 int msgctl(int msqid, int cmd, struct msqid_ds *buf); ** //Jack.c

//实现与rose的相互通信 #include #include #include #include #include #include #include #include #include #include #include //定义消息结构体 struct msgbuf { long mtype; char mtext[64]; }; int main(int argc, char *argv[]) { //定义key值 key_t key; key = ftok(".", 10); //根据key值申请消息队列ID号 int msgid; msgid = msgget(key , IPC_CREAT|0666); struct msgbuf buf; pid_t pid = fork(); //父进程持续发信息 if(pid > 0) { printf("I'm Jack.\n"); while(1) { memset(buf.mtext,0,sizeof(buf.mtext)); //写标识是1的信息进队列 buf.mtype = 1; fgets(buf.mtext,64,stdin); msgsnd(msgid,&buf,strlen(buf.mtext),0); //发送quit后,杀掉子进程,之后退出 if(strncmp(buf.mtext,"quit",4) == 0) { kill(pid,SIGKILL); break; } } wait(NULL); } //子进程持续读信息 if(pid == 0) { while(1) { memset(buf.mtext,0,sizeof(buf.mtext)); //从队列中读标识是2的信息 msgrcv(msgid,&buf,64,2,0); printf("receive from rose:%s",buf.mtext); //收到quit,杀掉父进程,之后退出 if(strncmp(buf.mtext,"quit",4) == 0) { kill(getppid(),SIGKILL); break; } } } //删除队列信息 msgctl(msgid, IPC_RMID, NULL); return 0; }

//Rose.c

#include #include #include #include #include #include #include #include #include #include #include //自定义消息结构体 struct msgbuf { long mtype; char mtext[64]; }; int main(int argc, char *argv[]) { //定义key值 key_t key; key = ftok(".", 10); //申请消息队列ID号 int msgid; msgid = msgget(key , IPC_CREAT|0666); struct msgbuf buf; pid_t pid = fork(); //父进程持续读信息 if(pid > 0) { printf("I'm Rose.\n"); while(1) { memset(buf.mtext,0,sizeof(buf.mtext)); //从队列中读标识是1的信息 msgrcv(msgid,&buf,64,1,0); printf("receive from jack:%s",buf.mtext); //收到quit后,杀掉子进程,之后退出 if(strncmp(buf.mtext,"quit",4) == 0) { kill(pid,SIGKILL); break; } } wait(NULL); } //子进程持续发信息 if(pid == 0) { while(1) { memset(buf.mtext,0,sizeof(buf.mtext)); //写标识是2的信息进队列 buf.mtype = 2; fgets(buf.mtext,64,stdin); msgsnd(msgid,&buf,strlen(buf.mtext),0); //发送quit后,杀掉父进程,之后退出 if(strncmp(buf.mtext,"quit",4) == 0) { kill(getppid(),SIGKILL); break; } } } //删除队列消息 msgctl(msgid, IPC_RMID, NULL); return 0; }

//运行结果

进程间通信 从代码可以看出,Jack和Rose都从队列中得到自己想要获得的信息,没有混淆,靠的就是数据的标识。

使用消息队列很简单,但是使用这种方式进行进程间通信,效率是不高的,因为两个进程间的数据传递并不是直接的,而是经过内核的辗转努力的,因此不适用于传输海量数据,而共享内存可以解决这个问题。

//还在学习中,如有错误,欢迎指出,谢谢 (´・ᴗ・`)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3